<?php
require '../config.php';
$idcliente = $_REQUEST['idcliente'];
if ($idcliente == null OR $idcliente == 0 OR empty($idcliente) )
{
	echo("<h3>Acceso negado :(</h3>");
	exit();
}
$query= "SELECT regpagos.idregpagos,
	   regpagos.idcliente,
       clientes.nombres,
       clientes.apellidos,
       clientes.cedula,
       regpagos.fecha,
       concat('$',FORMAT(regpagos.monto,2)) as monto,
       regpagos.observacion,
       regpagos.idusuario,
       usuarios.nombre,
       usuarios.apellido
  	   FROM (hossbd.regpagos regpagos INNER JOIN hossbd.usuarios usuarios ON (regpagos.idusuario = usuarios.idusuarios))
  	   INNER JOIN hossbd.clientes clientes ON (regpagos.idcliente = clientes.idclientes)
 	   WHERE (regpagos.idcliente = '".$idcliente."')
 	   ORDER BY regpagos.idregpagos DESC";

$resultset = mysql_query($query,$conexion);
if(!$resultset)
{
	mysql_close();
	echo json_encode('error en el query: ' . mysql_error());
	echo "<br><button type='button' class='btn btn-link btn-xs'  onclick='ver(". $idcliente .")'><span class='glyphicon glyphicon-arrow-left'></span> Regresar</button>";
}
elseif(!mysql_num_rows($resultset))
{
	mysql_close();
	echo json_encode('Sin resultados');
	echo "<br><button type='button' class='btn btn-link btn-xs'  onclick='ver(". $idcliente .")'><span class='glyphicon glyphicon-arrow-left'></span> Regresar</button>";
	
}
else
{
	$row = mysql_fetch_assoc($resultset);
	echo   "<table class='table table-bordered'>";
	echo   "<strong>Id Cliente:</strong> <span class='badge'>".$row['idcliente']."</span><strong> Nombre:</strong> <span class='badge'> "._convert($row['nombres'])." "._convert($row['apellidos'])."</span><strong> C&eacute;dula:</strong> <span class='badge'> ".$row['cedula']."</span>";
	echo "<br>";
	echo "<button type='button' class='btn btn-link btn-xs'  onclick='ver(". $row['idcliente'] .")'><span class='glyphicon glyphicon-arrow-left'></span> Regresar</button>";
	
	echo   "<tr class='success'>";
	echo   "<th >Id pago</th><th>Fecha</th><th>Monto</th><th>Observaci&oacute;n</th><th>Id usuario</th><th>Usuario</th><th colspan='2'>Opciones</th>";
	echo   "</tr>";
	echo   "<tbody>";
	$aux=0;
	do
	{
		if($aux==0){
			$color="#FFFFFF";
			$aux=1;
		}else{
			$color="#F9F9F9";
			$aux=0;
		}
		echo ("<tr bgcolor='".$color."'>");
		echo   "<td>".$row['idregpagos']."</td>";
		echo   "<td>".date("F j, Y",strtotime($row['fecha']))."</td>";
		echo   "<td>".$row['monto']."</td>";
		echo   "<td>".$row['observacion']."</td>";
		echo   "<td>".$row['idusuario']."</td>";
		echo   "<td>".$row['nombre']." ".$row['apellido']."</td>";
		$result2 = mysql_query("select p.actualizar,p.eliminar from permisos p,modulos m where p.idmodulo = m.idmodulos and p.idrol= '". $_SESSION['rol'] ."' and m.nombre = 'Clientes'",$conexion);
		if(mysql_num_rows($result2)>0)
		{
			$row2 = mysql_fetch_assoc($result2);
			if($row2['actualizar']== 1 and $row2['eliminar']== 1)
			{
				echo   "<td><button type='button' class='btn btn-link btn-xs'  onclick='modificarPago(". $row['idregpagos'] .")'><span class='glyphicon glyphicon-pencil'></span> Modificar</button></td>";
				echo   "<td><button type='button' class='btn btn-link btn-xs'  onclick='eliminarPago(". $row['idregpagos'] .")'><span class='glyphicon glyphicon-remove'></span> Eliminar</button></td>";
			
			}
			else if($row2['actualizar']== 1 and $row2['eliminar']== 0)
			{
				echo   "<td><button type='button' class='btn btn-link btn-xs'  onclick='modificarPago(". $row['idregpagos'] .")'><span class='glyphicon glyphicon-pencil'></span> Modificar</button></td>";
				echo   "<td></td>";
		
					
			}
			else if($row2['actualizar']== 0 and $row2['eliminar']== 1)
			{
				echo   "<td></td>";
				echo   "<td><button type='button' class='btn btn-link btn-xs'  onclick='eliminarPago(". $row['idregpagos'] .")'><span class='glyphicon glyphicon-remove'></span> Eliminar</button></td>";
		
		
			}
			else echo"<td></td>";
		}
		echo   "</tr>";
	}
	while($row = mysql_fetch_assoc($resultset));
	echo   "</tbody>";
	echo   "</table>";
	mysql_close();
}
function _convert($content) {
	if(!mb_check_encoding($content, 'UTF-8')
	OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

		$content = mb_convert_encoding($content, 'UTF-8');

		if (mb_check_encoding($content, 'UTF-8')) {
			// log('Converted to UTF-8');
		} else {
			// log('Could not converted to UTF-8');
		}
	}
	return $content;
}
?>